package com.mycjj.android.activity;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.utils.UIHandler;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.R;
import com.mycjj.android.bean.BaseBean;
import com.mycjj.android.bean.SettingBaseBean;
import com.mycjj.android.bean.SettingBaseBean.SettingData.ShareBean;
import com.mycjj.android.bean.TreeElement;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.share.OnekeyShare;
import com.mycjj.android.util.Preferences;

//分享领元宝
public class Score_ShareGetGoldActivity extends BaseActivity implements Callback{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_score_share_get_gold);
		ShareSDK.initSDK(this);
		initHeader();
		init();
		initData();
	}

	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;

	private void initHeader() {
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("分享领元宝");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Score_ShareGetGoldActivity.this.finish();
			}
		});
	}

	private TextView tv_item1, tv_item2, tv_item3, tv_item4, tv_item5;
	private View mLl_panel_b, mLl_panel_a;
	private View mLv_close_a, mLv_close_b;

	private Button mBtn_01, mBtn_02;

	private TextView mEt_name, mEt_tel,  mTv_brand;
	private Spinner mSpinner_type;
	private Button mBtn_submmit_a;
	private int currentType = 1;
	private TreeElement element;
	private void init() {
		mBtn_01 = (Button) findViewById(R.id.btn_01);
		mBtn_02 = (Button) findViewById(R.id.btn_02);
		mBtn_01.setOnClickListener(showPanelListener);
		mBtn_02.setOnClickListener(showPanelListener);

		tv_item1 = (TextView) findViewById(R.id.tv_item1);
		tv_item2 = (TextView) findViewById(R.id.tv_item2);
		tv_item3 = (TextView) findViewById(R.id.tv_item3);
		tv_item4 = (TextView) findViewById(R.id.tv_item4);
		tv_item5 = (TextView) findViewById(R.id.tv_item5);

		mLl_panel_b = findViewById(R.id.ll_panel_b);
		mLl_panel_a = findViewById(R.id.ll_panel_a);

		mLv_close_a = findViewById(R.id.iv_close_a);
		mLv_close_b = findViewById(R.id.iv_close_b);
		mEt_name = (TextView) findViewById(R.id.et_name);
		mEt_tel = (TextView) findViewById(R.id.et_tel);
		mSpinner_type = (Spinner) findViewById(R.id.spinner_type);
		mTv_brand = (TextView) findViewById(R.id.tv_brand);
		mTv_brand.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Score_ShareGetGoldActivity.this,BrandActivity.class);
				startActivity(intent);
			}
		});
		mBtn_submmit_a = (Button) findViewById(R.id.btn_submmit_a);
		mLv_close_a.setOnClickListener(closePanelListener);
		mLv_close_b.setOnClickListener(closePanelListener);
		mBtn_submmit_a.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				submit_a();
			}
		});
		String[] mItems = getResources().getStringArray(R.array.type_array);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mItems);
		mSpinner_type.setAdapter(adapter);
		
		mSpinner_type.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				currentType = arg2 + 1;
				Log.e("mark", "type:"+currentType);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		element = dataManager.getElement();
		if(element != null){
			mTv_brand.setText(element.getTitle());
		}
	}
	
	private void submit_a(){
		String name = mEt_name.getText().toString().trim();
		if(TextUtils.isEmpty(name)){
			Toast.makeText(this, "姓名不能为空", Toast.LENGTH_LONG).show();
			return;
		}
		
		String tel = mEt_tel.getText().toString().trim();
		if(TextUtils.isEmpty(tel)){
			Toast.makeText(this, "手机号不能为空", Toast.LENGTH_LONG).show();
			return;
		}
		
		if(element == null){
			Toast.makeText(mApp, "请选择品牌", Toast.LENGTH_SHORT).show();
			return;
		}
		
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("name", name);
		params.put("phone", tel);
		params.put("cost_type", String.valueOf(currentType));
		params.put("brand1", element.getBrandId());
		params.put("brand2", element.getBrandId1());
		params.put("brand3", element.getBrandId2());
		params.put("brand4", element.getBrandId3());
		params.put("client_id",ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		request.requsetSyceeClueWithDoneHandler("",params,new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				BaseBean<Map> baseBean = (BaseBean<Map>)dataManager.getBaseBean();
				if(baseBean != null){
					if(baseBean.getCode() == 200){
						clearText();
						mLl_panel_a.setVisibility(View.GONE);
						Toast.makeText(Score_ShareGetGoldActivity.this, "提供线索提交成功!", Toast.LENGTH_SHORT).show();
					}else{
						Toast.makeText(Score_ShareGetGoldActivity.this, baseBean.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}else{
					Log.e("mark", "BaseBean is null!!");
				}
			}
		});
	}
	
	private void clearText(){
		mEt_name.setText("");
		mEt_tel.setText("");
		element = null;
		mTv_brand.setText("");
	}

	private OnClickListener showPanelListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_01:
				mLl_panel_a.setVisibility(View.VISIBLE);
				break;
			case R.id.btn_02:
				showShare(false, null);
//				mLl_panel_b.setVisibility(View.VISIBLE);
				break;
			}
		}
	};
	private OnClickListener closePanelListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.iv_close_a:
				mLl_panel_a.setVisibility(View.GONE);
				break;
			case R.id.iv_close_b:
				mLl_panel_b.setVisibility(View.GONE);
				break;
			}
		}
	};
	SettingBaseBean settingBaseBean;
	
	private void initData() {
		settingBaseBean = dataManager.getSettingBaseBean();
		if (settingBaseBean == null) {
			request = new ProjectHttpRequestImpl(this, true);
			RequestParams params = new RequestParams();
			params.put("client_id", ProjectHttpRequestInterface.client_id_key);
			String token = Preferences.getSharedPreferences(this, "user_token",
					"");
			params.put("token", token);
			request.requestSyceeGetSettingWithDoneHandler("", params,
					new AsyncHttpResponseHandler() {
						@Override
						public void onSuccess(String content) {
							settingBaseBean = dataManager.getSettingBaseBean();
							if (settingBaseBean != null) {
								if (settingBaseBean.getCode() == 200) {
									initView();
								} else {
									Toast.makeText(
											Score_ShareGetGoldActivity.this,
											"code:" + settingBaseBean.getCode()
													+ ";message:"
													+ settingBaseBean.getMsg(),
											Toast.LENGTH_SHORT).show();
								}
							} else {
								Log.e("mark", "settingBaseBean is null!!");
							}
						}
					});
		} else {
			initView();
		}

	}

	private void initView() {
		ShareBean bean = settingBaseBean.getData().getShare();
		if (bean != null) {
			tv_item1.setText(bean.getRegist());// 好友注册
			tv_item2.setText(bean.getBuy_car());// 好友买车
			tv_item3.setText(bean.getRepair());// 好友维修保养
			tv_item4.setText(bean.getFacial());// 好友汽车美容
			tv_item5.setText(bean.getBuy_parts());// 购买零配件
		}
	}
	
	private void showShare(boolean silent, String platform) {
		final OnekeyShare oks = new OnekeyShare();
		oks.setNotification(R.drawable.ic_launcher, this.getString(R.string.app_name));
		oks.setAddress("12345678901");
		oks.setTitle("欢迎注册成为车佳家用户，领取元宝参与抽奖，惊喜多多！");
		oks.setTitleUrl("http://www.baidu.com");
		oks.setText("欢迎注册成为车佳家用户，领取元宝参与抽奖，惊喜多多！");
//		oks.setImagePath(MainActivity.TEST_IMAGE);
//		oks.setImageUrl(MainActivity.TEST_IMAGE_URL);
		oks.setUrl("http://www.baidu.com");
		oks.setComment("欢迎注册成为车佳家用户，领取元宝参与抽奖，惊喜多多！");
//		oks.setSite(getContext().getString(R.string.app_name));
//		oks.setSiteUrl("http://sharesdk.cn");
		oks.setVenueName("ShareSDK");
		oks.setVenueDescription("This is a beautiful place!");
//		oks.setLatitude(23.056081f);
//		oks.setLongitude(113.385708f);
		oks.setSilent(silent);
		if (platform != null) {
			oks.setPlatform(platform);
		}

		// 取消注释，可以实现对具体的View进行截屏分享
//		oks.setViewToShare(getPage());

		// 去除注释，可令编辑页面显示为Dialog模式
//		oks.setDialogMode();

		// 去除注释，在自动授权时可以禁用SSO方式
//		oks.disableSSOWhenAuthorize();

		// 去除注释，则快捷分享的操作结果将通过OneKeyShareCallback回调
		oks.setCallback(new mPlatformActionListener());
//		oks.setShareContentCustomizeCallback(new ShareContentCustomizeDemo());

		// 去除注释，演示在九宫格设置自定义的图标
//		Bitmap logo = BitmapFactory.decodeResource(menu.getResources(), R.drawable.ic_launcher);
//		String label = menu.getResources().getString(R.string.app_name);
//		OnClickListener listener = new OnClickListener() {
//			public void onClick(View v) {
//				String text = "Customer Logo -- ShareSDK " + ShareSDK.getSDKVersionName();
//				Toast.makeText(menu.getContext(), text, Toast.LENGTH_SHORT).show();
//				oks.finish();
//			}
//		};
//		oks.setCustomerLogo(logo, label, listener);

		// 去除注释，则快捷分享九宫格中将隐藏新浪微博和腾讯微博
//		oks.addHiddenPlatform(SinaWeibo.NAME);
//		oks.addHiddenPlatform(TencentWeibo.NAME);

		oks.show(this);
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch(msg.what) {
		case 1: {
			Toast.makeText(this, "取消分享", Toast.LENGTH_SHORT).show();
		}
		break;
		case 2: {
			Toast.makeText(this, "分享成功", Toast.LENGTH_SHORT).show();
		}
		break;
		case 3: {
			Toast.makeText(this, "分享失败", Toast.LENGTH_SHORT).show();
		}
		
		break;
	}
		return false;
	}
	
	//授权等回调方法
	public class mPlatformActionListener implements PlatformActionListener{
		@Override
		public void onCancel(Platform arg0, int action) {
			//取消授权
			if (action == Platform.ACTION_USER_INFOR) {
				UIHandler.sendEmptyMessage(1, Score_ShareGetGoldActivity.this);
			}
		}
		@Override
		public void onComplete(Platform platform, int action, HashMap<String, Object> res) {
			// 成功
			if (action == Platform.ACTION_USER_INFOR) {
				UIHandler.sendEmptyMessage(2, Score_ShareGetGoldActivity.this);
			}
		}
		@Override
		public void onError(Platform arg0, int action, Throwable arg2) {
			//失败！
			if (action == Platform.ACTION_USER_INFOR) {
				UIHandler.sendEmptyMessage(3, Score_ShareGetGoldActivity.this);
			}
			arg2.printStackTrace();
		}
	}
}
